#ifndef _PKT_PROXY_H_
#define _PKT_PROXY_H_

//* Node 2
static const unsigned char riscv_mac[6] = {0x52, 0x49, 0x53, 0x43, 0x02, 0x56};
static const unsigned int riscv_ip = 0x0201A8C0;

/* Node 3
 static const unsigned char riscv_mac[6] = {0x52, 0x49, 0x53, 0x43, 0x03, 0x56};
 static const unsigned int riscv_ip = 0x0301A8C0;
*/

// Node 2
static unsigned char arp_reply_pkt[60] = {
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x52, 0x49, 0x53, 0x43, 0x02, 0x56, 0x08, 0x06, 0x00, 0x01, 
    0x08, 0x00, 0x06, 0x04, 0x00, 0x02, 0x52, 0x49, 0x53, 0x43, 0x02, 0x56, 0xC0, 0xA8, 0x01, 0x02, 
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
};

static unsigned char udp_send_pkt[1600] = {
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x52, 0x49, 0x53, 0x43, 0x02, 0x56, 0x08, 0x00, 0x45, 0x00, 
    0x00, 0x00, 0x88, 0x02, 0x00, 0x00, 0x80, 0x11, 0x00, 0x00, 0xC0, 0xA8, 0x01, 0x02, 0x00, 0x00, 
    0x00, 0x00, 0x22, 0xB8, 0x22, 0xB8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
};


// Node 3
/*
static unsigned char arp_reply_pkt[60] = {
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x52, 0x49, 0x53, 0x43, 0x03, 0x56, 0x08, 0x06, 0x00, 0x01, 
    0x08, 0x00, 0x06, 0x04, 0x00, 0x02, 0x52, 0x49, 0x53, 0x43, 0x03, 0x56, 0xC0, 0xA8, 0x01, 0x03, 
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
};

static unsigned char udp_send_pkt[1600] = {
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x52, 0x49, 0x53, 0x43, 0x03, 0x56, 0x08, 0x00, 0x45, 0x00, 
    0x00, 0x00, 0x88, 0x03, 0x00, 0x00, 0x80, 0x11, 0x00, 0x00, 0xC0, 0xA8, 0x01, 0x03, 0x00, 0x00, 
    0x00, 0x00, 0x22, 0xB8, 0x22, 0xB8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
};
*/

struct eth_hdr {
    unsigned char   dmac[6];
    unsigned char   smac[6];
    unsigned short  eth_type;
}__attribute__((packed));

struct ip_hdr {
    unsigned char   version:4,
                    ihl:4;
    unsigned char   tos;
    unsigned short  tot_len;
    unsigned short  id;
    unsigned short  frag_offset;
    unsigned char   ttl;
    unsigned char   protocol;
    unsigned short  checksum;
    unsigned int    sip;
    unsigned int    dip;
}__attribute__((packed));

struct udp_hdr {
    unsigned short  sport;
    unsigned short  dport;
    unsigned short  length;
    unsigned short  checksum;
}__attribute__((packed));

struct ip_pkt_hdr {
    struct eth_hdr  eth_hdr_part;
    struct ip_hdr   ip_hdr_part;
}__attribute__((packed));

struct udp_pkt_hdr {
    struct eth_hdr  eth_hdr_part;
    struct ip_hdr   ip_hdr_part;
    struct udp_hdr  udp_hdr_part;
}__attribute__((packed));

unsigned short htons(unsigned short number);
unsigned int htonl(unsigned int number);

unsigned short ip_check_sum(unsigned short * pkt, unsigned short checksum_old);
unsigned short udp_check_sum(unsigned short * pkt, unsigned short checksum_old, unsigned short ip_pktlen, unsigned short udp_pktlen);
unsigned int arp_reply(unsigned char * request_pkt, unsigned char * reply_pkt);
unsigned int udp_relay_tsmp(unsigned char * pkt, unsigned char * udp_pkt, unsigned int pkt_len);
unsigned int tsmp_relay_udp(unsigned char * pkt, unsigned char * udp_pkt, unsigned int pkt_len);
unsigned short ip_csum(unsigned short * packet, int packlen);

#endif
